﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.DataAccess;
using University.Workshop.Model;
using System.Text.RegularExpressions;
using System.Windows.Forms;

[assembly: CLSCompliant(true)]
namespace University.Workshop.Business
{
     public static class AdministratorManager
    {
         public static void ValidateAdministrator(string userName,string password,string fullName,string surname,string email)
         { 
             if (string.IsNullOrEmpty(userName)||string.IsNullOrEmpty(password)||string.IsNullOrEmpty(fullName)||string.IsNullOrEmpty(surname)||string.IsNullOrEmpty(email))
             {
                 throw new ArgumentException("All inputs are required.");
             }
             OperatorRegister admi = new OperatorRegister();
             admi.Dispose();
             if (admi.ExistOperatorByPassword(password) && admi.ExistOperatorByUserName(userName))
                throw new ArgumentException("The operator exist");
             
             if (!Regex.IsMatch(password, @"^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#_$%^&+=]).*$"))
                 throw new ArgumentException(@"La contraseña debe tener un minimo de 8 caracteres. La contraseña debe contener minusculas, mayusculas, y al menos un caracter especial. Los caracteres especiales admitidos son: @#_$%^&+= ");

             if (!Regex.IsMatch(email, @"^([\w\d\-\.]+)@{1}(([\w\d\-]{1,67})| ([\w\d\-]+\.[\w\d\-]{1,67}))\.(([a-zA-Z\d]{2,4})(\.[a-zA-Z\d]{2})?)$"))
                 throw new ArgumentException("the format the email is incorrect");
             if (userName.Length > 15 || userName.Length < 5)
                 throw new ArgumentException("User Name : the maximum length is 15 and the minimum is 5");
             if (surname.Length > 15 || surname.Length < 5)
                 throw new ArgumentException("Surname : the maximum length is 15 and the minimum is 5");
             if (fullName.Length>15 ||fullName.Length<5)
                 throw new ArgumentException("Name: the maximum length is 15 and the minimum is 5");
           
  
             admi.RegisterOperator(new Administrator { Name=fullName,Surname=surname,UserName=userName,Password=password,Email=email});
             TripleDesManager DesManager = new TripleDesManager();
             TripleDESUtil encriptado = DesManager.Encriptar(password);
             TripelDesDataAccess dal = new TripelDesDataAccess();
             dal.Dispose();
             dal.GuardarenBD(encriptado,userName);
         }
         public static void ValidateForUpdateAdministrator(string user,string pass,string userName, string password, string fullName, string surname, string email)
         {
             if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fullName) || string.IsNullOrEmpty(surname) || string.IsNullOrEmpty(email))
             {
                 throw new ArgumentException("All inputs are required.");
             }
             OperatorRegister admi = new OperatorRegister();
             admi.Dispose();
             if (!string.Equals(user, userName))
             {
                 if (admi.ExistOperatorByUserName(userName))
                     throw new ArgumentException("The operator exist");
             }
             if (userName.Length > 15 || userName.Length < 5)
                 throw new ArgumentException("user Name : the maximum length is 15 and the minimum is 5");
             if (surname.Length > 15 || surname.Length < 5)
                 throw new ArgumentException("Last Name : the maximum length is 15 and the minimum is 5");
             if (fullName.Length > 15 || fullName.Length < 5)
                 throw new ArgumentException("Name: the maximum length is 15 and the minimum is 5");

             if (!string.Equals(password,pass))
                if (admi.ExistOperatorByPassword(password)){    throw new ArgumentException("There is an administrator with the same password ");}   
             
             if (!Regex.IsMatch(password, @"^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#_$%^&+=]).*$"))
                throw new ArgumentException(@"La contraseña debe tener un minimo de 8 caracteres. La contraseña debe contener minusculas, mayusculas, y al menos un caracter especial. Los caracteres especiales admitidos son: @#_$%^&+= ");
             
             if (!Regex.IsMatch(email, @"^([\w\d\-\.]+)@{1}(([\w\d\-]{1,67})| ([\w\d\-]+\.[\w\d\-]{1,67}))\.(([a-zA-Z\d]{2,4})(\.[a-zA-Z\d]{2})?)$")){  throw new ArgumentException("the format the email is incorrect");}
             UpdateAdministrator updateAdministrator = new UpdateAdministrator();
             updateAdministrator.Dispose();
             updateAdministrator.UpdateByUserName(user, pass, new Administrator { Name=fullName,Surname=surname,UserName=userName,Password=password,Email=email});
         }
         public static void GetAllAdministrators(DataGridView administrator) 
         {
             UpdateAdministrator admi = new UpdateAdministrator();
             admi.Dispose();
             admi.GetAllAdministrators(administrator);
         }
         public static Administrator GetAdministratorByUserName(string userName,string password) 
         {
             try
             {
                 UpdateAdministrator updateAdministrator = new UpdateAdministrator();
                 updateAdministrator.Dispose();
                 return updateAdministrator.GetOperatorByUserName(userName, password);
             }
             catch (Exception ex)
             {
                 throw new ArgumentException(ex.Message);
             }
         }
    }
}
